package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiU_discover_comment;

import cn.com.api.service.ApiU_discover_commentService;

import cn.com.util.*;


public class U_discover_commentBaseController {

	private static Logger logger = Logger.getLogger(U_discover_commentBaseController.class);
	
	@Autowired
	private ApiU_discover_commentService apiU_discover_commentService;
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "u_discover_comment-list")
	public ModelAndView u_discover_comment_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_discover_comment_list.jsp");
		
		List<ApiU_discover_comment> u_discover_comments = apiU_discover_commentService.findU_discover_comments();
		mv.addObject("u_discover_comments", u_discover_comments);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "u_discover_comment-add-pre")
	public ModelAndView u_discover_comment_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_discover_comment_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			ApiU_discover_comment u_discover_comment = apiU_discover_commentService.findU_discover_commentById(Integer.valueOf(id));
			mv.addObject("u_discover_comment",u_discover_comment);
		}
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "u_discover_comment-add")
	public void u_discover_comment_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String comment_id = request.getParameter("comment_id");
		String title = request.getParameter("title");
		String forum_title = request.getParameter("forum_title");
		String content = request.getParameter("content");
		String create_time = request.getParameter("create_time");
		String type = request.getParameter("type");
		String target_id = request.getParameter("target_id");
		String user_id = request.getParameter("user_id");
		String user_name = request.getParameter("user_name");
		String if_effect = request.getParameter("if_effect");
		String is_delete = request.getParameter("is_delete");
		String relay_id = request.getParameter("relay_id");
		String reply_count = request.getParameter("reply_count");
		String good_count = request.getParameter("good_count");
		String bad_count = request.getParameter("bad_count");
		String click_count = request.getParameter("click_count");
		String fav_count = request.getParameter("fav_count");
		
		Map<String,Object> map = new HashMap<String,Object>();
		//String id = request.getParameter("id");
		//add
		if(comment_id==null || (comment_id.trim()).equals("")){
			ApiU_discover_comment u_discover_comment = new ApiU_discover_comment();
            /*
		    u_discover_comment.setComment_id(comment_id);
		    u_discover_comment.setTitle(title);
		    u_discover_comment.setForum_title(forum_title);
		    u_discover_comment.setContent(content);
		    u_discover_comment.setCreate_time(create_time);
		    u_discover_comment.setType(type);
		    u_discover_comment.setTarget_id(target_id);
		    u_discover_comment.setUser_id(user_id);
		    u_discover_comment.setUser_name(user_name);
		    u_discover_comment.setIf_effect(if_effect);
		    u_discover_comment.setIs_delete(is_delete);
		    u_discover_comment.setRelay_id(relay_id);
		    u_discover_comment.setReply_count(reply_count);
		    u_discover_comment.setGood_count(good_count);
		    u_discover_comment.setBad_count(bad_count);
		    u_discover_comment.setClick_count(click_count);
		    u_discover_comment.setFav_count(fav_count);
	        */
			apiU_discover_commentService.insertU_discover_comment(u_discover_comment);
		}
		//edit
		else{
			ApiU_discover_comment u_discover_comment = apiU_discover_commentService.findU_discover_commentById(Integer.valueOf(comment_id));
	        /*
		    u_discover_comment.setComment_id(comment_id);
		    u_discover_comment.setTitle(title);
		    u_discover_comment.setForum_title(forum_title);
		    u_discover_comment.setContent(content);
		    u_discover_comment.setCreate_time(create_time);
		    u_discover_comment.setType(type);
		    u_discover_comment.setTarget_id(target_id);
		    u_discover_comment.setUser_id(user_id);
		    u_discover_comment.setUser_name(user_name);
		    u_discover_comment.setIf_effect(if_effect);
		    u_discover_comment.setIs_delete(is_delete);
		    u_discover_comment.setRelay_id(relay_id);
		    u_discover_comment.setReply_count(reply_count);
		    u_discover_comment.setGood_count(good_count);
		    u_discover_comment.setBad_count(bad_count);
		    u_discover_comment.setClick_count(click_count);
		    u_discover_comment.setFav_count(fav_count);
			*/
			apiU_discover_commentService.updateU_discover_comment(u_discover_comment);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
        JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	/**
	 *删除
	 * @return
	 */
	@RequestMapping(value = "u_discover_comment-delete")
	public void u_discover_comment_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		//delete
		int ret_i =	apiU_discover_commentService.deleteU_discover_comment(Integer.valueOf(id));
		
		//
		if(ret_i > 0){
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
}



